package com.coocaa.ops.admin.v1.timedtask.job;


import com.coocaa.ops.admin.async.MyAsyncOpsMgrServiceImpl;
import com.coocaa.ops.admin.base.properties.SystemProperties;
import com.coocaa.ops.admin.v1.timedtask.biz.TimedTaskBizImpl;
import com.coocaa.ops.admin.v1.timedtask.constant.TimedTaskTypeConstants;
import com.coocaa.ops.admin.v1.timedtask.job.impl.MyQuartzJobServiceImpl;
import com.coocaa.ops.admin.v1.timedtask.service.impl.TimedTaskServiceImpl;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.timedtask.entity.TimedTaskInfoEntity;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author bijiahao
 * @date : 2017/12/19.
 * @description
 */

@Component
public class LoadJob implements InitializingBean {
    @Autowired
    private MyQuartzJobServiceImpl myQuartzJobService;
    @Autowired
    private TimedTaskServiceImpl timedTaskService;
    @Autowired
    private MyAsyncOpsMgrServiceImpl myAsyncOpsMgrService;
    @Autowired
    private TimedTaskBizImpl timedTaskBiz;
    @Autowired
    private SystemProperties systemProperties;


    @Override
    public void afterPropertiesSet() throws Exception {
        if (null != systemProperties.getEnableInitTimedJob() && systemProperties.getEnableInitTimedJob()) {
            //初始化定时任务-版面强插
//            this.initJobForTabInsert();
            // 初始化定时任务-定时上架
            this.initJobForTimelyShelves();
            //初始化定时任务-推荐池更新
//            this.initJobForRecommendPoolUpdate();
            //初始化定时任务-推荐流AB测试
//            this.initRecStreamAbTest();
        }
    }



    /**
     * 初始化定时任务-定时上架
     */
    private void initJobForTimelyShelves() {
        QueryTemplate<TimedTaskInfoEntity> wrapper = new QueryTemplate<>();
        wrapper.eq("timed_type", TimedTaskTypeConstants.TIMELY_SHELVING)
                .eq("status", ResourceStatusEnums.AUDITED_NOT_ON_LINE.getStatus())
        ;
        List<TimedTaskInfoEntity> taskInfoList = timedTaskBiz.getMapper().selectList(wrapper);
        for (TimedTaskInfoEntity timedTask : taskInfoList) {
            handleEachTimedTask(timedTask);
        }
    }




    /**
     * 处理每一个定时任务
     *
     * @param timedTask
     */
    private void handleEachTimedTask(TimedTaskInfoEntity timedTask) {
        long currentTime = System.currentTimeMillis();
        Date releaseDate = timedTask.getReleaseTime();
        long releaseTime = releaseDate.getTime();
        if (releaseTime > currentTime) {
            //如果定时时间还没到，就进行任务添加
            myQuartzJobService.addJobForTimelyShelves(timedTask, TimelyShelvesQuartzJob.class);
        } else {//如果定时时间已到
            boolean isOk = timedTaskService.execPutShelvesAndUpdateStatus(timedTask);
            if (isOk) {
                //执行异步操作
                myAsyncOpsMgrService.handleCommonAsyncOps(ResourceBaseModel.getResourceBaseModel(timedTask.getResourceType(), timedTask.getResourceId(), timedTask.getResourceVersion()), timedTask.getCurrentOperator());
            }
        }
    }

}
